#!/bin/bash

set -e

if [ -z "$DEPENDABOT_NATIVE_HELPERS_PATH" ]; then
  echo "Unable to build, DEPENDABOT_NATIVE_HELPERS_PATH is not set"
  exit 1
fi

helpers_dir=$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)
install_dir="${DEPENDABOT_NATIVE_HELPERS_PATH:?}/julia"

mkdir -p "$install_dir"

# Copy helper files to the installation directory
cp -r "$helpers_dir"/* "$install_dir"

# Set Julia depot path to ensure packages are compiled to a shared location
# Trailing : is intentional. It automatically includes the bundled stdlibs
export JULIA_DEPOT_PATH="${HOME:?}/.julia:"

# Set up Julia package environment
julia --project="$install_dir" -e 'import Pkg; Pkg.instantiate()'

echo "Julia helpers installed successfully in $install_dir"
echo "Julia depot path: $JULIA_DEPOT_PATH"
